﻿using AntMovement.Core;
using AntMovement.Core.Entity;
using AntMovement.Core.Hardware;
using AntMovement.Core.IParameter;
using AntMovement.Core.Logic;
using AntMovement.Core.Model;
using AntMovement.Core.Tool;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AntMovement.Parameter
{
    public class AxisParameter : InitFileBase, IAxisParameter
    {
        public List<PointPositionInfoModel> PointPosition { get; set; }
        public List<Axis> AxisDataSet { get; set; }
        
        public AxisParameter() : base("init", "axisparameter")
        {
            if (System.IO.File.Exists(_path))
            {
                string str = base.Read();
                if (!string.IsNullOrEmpty(str))
                {
                    Dictionary<string, object> keyValues = System.Text.Json.JsonSerializer.Deserialize<Dictionary<string, object>>(str);
                    try
                    {
                        AxisDataSet = System.Text.Json.JsonSerializer.Deserialize<List<Axis>>(keyValues["AxisDataSet"].ToString());
                        PointPosition = System.Text.Json.JsonSerializer.Deserialize<List<PointPositionInfoModel>>(keyValues["PointPosition"].ToString());
                    }
                    catch (Exception)
                    {
                    }
                }
            }
            if (AxisDataSet == null)
            {
                AxisDataSet = new List<Axis>();
            }
            if (PointPosition == null)
            {
                PointPosition = new List<PointPositionInfoModel>() 
                {
                    new PointPositionInfoModel()
                    {
                        Name = "推料盘位",
                    },
                    new PointPositionInfoModel()
                    { 
                        Name = "收料盘位",
                    },
                    new PointPositionInfoModel()
                    {
                        Name = "标定点位",
                    },
                    new PointPositionInfoModel()
                    {
                        Name = "码垛1起点位",
                    },
                    new PointPositionInfoModel()
                    {
                        Name = "码垛2起点位",
                    },
                };
            }
        }

        public bool Save()
        {
            Dictionary<string, object> data = new Dictionary<string, object>();
            data.Add("AxisDataSet", AxisDataSet);
            data.Add("PointPosition", PointPosition);
            return Save(System.Text.Json.JsonSerializer.Serialize(data, OptionsHelper.Options));
        }

        public IAxis GetAxis(string text)
        {
            Axis axis = AxisDataSet.FirstOrDefault(x => x.Name == text);
            if (axis != null)
            {
                switch (axis.Type)
                {
                    case Core.Enum.MotionType.PCI9016: return new AntMovement.PCI9016.Logic.AxitHardware(axis);
                    case Core.Enum.MotionType.SolidTech_E_SerialCard: return new AntMovernent.SolidTechESerialCard.Logic.AxitHardware(axis);
                    case Core.Enum.MotionType.SMC308: return new AntMovement.SDK.LTSMC.Logic.AxitHardware(axis);
                    case Core.Enum.MotionType.SMC304: return new AntMovement.SDK.LTSMC.Logic.AxitHardware(axis);
                }
            }
            return default;
        }
        private IList<AxisMotion> _axisMotions = new List<AxisMotion>();
        public IList<AxisMotion> GetAxisAll() => _axisMotions;

        public AxisMotion GetAxisMotion(string text) => _axisMotions.Where(x => x.Text == text).FirstOrDefault();

        public void InitAxisMotion(ILog log)
        {
            _axisMotions.Clear();
            foreach (var item in AxisDataSet)
            {
                _axisMotions.Add(new AxisMotion(this, item.Name, log));
            }
        }

        public Axis GetAxisEntity(string text) => AxisDataSet.FirstOrDefault(x => x.Name == text);

        public PointPositionInfoModel GetPointPosition(string name) => PointPosition.FirstOrDefault(x => x.Name == name);
    }
}
